<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
            <el-form-item label="描述：">
                <el-input v-model="form.description"></el-input>
            </el-form-item>
            <el-form-item label="属性名：">
                <el-input v-model="form.attributeName" placeholder="解析到json中的key值,不要存在相同属性"></el-input>
            </el-form-item>
            <el-form-item label="属性类型：">
                <el-select v-model="form.attributeType" style="height: auto;">
                    <el-option v-for="dict in attributeTypeOptions"
                               :key="dict.dictValue"
                               :label="dict.dictLabel"
                               :value="dict.dictValue">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="属性结果类型：">
                <el-select v-model="form.attributeResultType" style="height: auto;">
                    <el-option v-for="dict in attributeResultTypeOptions"
                               :key="dict.dictValue"
                               :label="dict.dictLabel"
                               :value="dict.dictValue">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="属性解析表达式：">
                <el-input v-model="form.express" placeholder="例如 8-10或recordType.waterLevel==1?0-8:null"></el-input>
            </el-form-item>
            <el-form-item label="分组字段：">
                <el-input v-model="form.groupField" placeholder="不分组不填"></el-input>
            </el-form-item>
            <el-form-item label="属性默认值：">
                <el-input v-model="form.value" placeholder="没有则不填"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addParseAttribute, updateParseAttribute} from '/src/api/driver/parseTcp'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            attributeTypeOptions: Array,
            attributeResultTypeOptions: Array,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    description: [
                        {required: true, message: "描述不能为空", trigger: "blur"}
                    ],
                    attributeName: [
                        {required: true, message: "属性名不能为空", trigger: "blur"}
                    ],
                    attributeType: [
                        {required: true, message: "属性类型不能为空", trigger: "blur"}
                    ],
                    attributeResultType: [
                        {required: true, message: "属性结果类型不能为空", trigger: "blur"}
                    ],
                    express: [
                        {required: true, message: "属性解析表达式", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.attributeId != undefined) {
                            updateParseAttribute(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addParseAttribute(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            }
        }
    }
</script>

<style scoped>

</style>
